package ldkxingzhe.ldkxingzhedemo.others;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ldkxingzhe on 2016/4/25.
 */
public class MessageService extends Service{
    private static final String TAG = "MessageService";
    List<ICallback> mCallBackList = new ArrayList<>();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MessageImpl();
    }

    private void distachMessage(){
        if(mCallBackList.size() < 2) return;
        for(ICallback iCallback : mCallBackList){
            try {
                iCallback.onMessage("ldkxingzhe");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private class MessageImpl extends IMessage.Stub{

        @Override
        public void register(ICallback callback) throws RemoteException {
            mCallBackList.add(callback);
            distachMessage();
        }
    }
}
